--USE [scm_main]
if exists (select 1
          from sysobjects
          where  id = object_id('bd_proc_auto_repair_attendance')
          and type in ('P','PC'))
DROP PROCEDURE bd_proc_auto_repair_attendance
GO
CREATE PROCEDURE [dbo].[bd_proc_auto_repair_attendance]
as
BEGIN

-- 关闭打印受影响行数
SET NOCOUNT ON;

		create table #user_serial(
			id int identity,
			user_serial varchar(50),
			user_name varchar(50)
		)

	--  异常扑捉机制
		BEGIN TRY


		-- 开启事务
			BEGIN TRAN

				declare @id int;
				declare @number varchar(50);

				declare @date date;
				declare @user_count int;


				-- 获取补勤ID和编号
				exec bd_proc_sys_getMaxId 11,@id output
				exec bd_proc_sys_getMaxNumber 11,@number output


				-- 获取高层用户编号
				insert into #user_serial(user_serial,user_name)
				select distinct user_serial,user_lname from dt_user where kqdkLx = '23'


				-- 获取当前日期
				set @date = getdate();

				-- 用户个数
				select @user_count = isnull(count(0),0) from #user_serial

				IF @user_count > 0 BEGIN

					-- 生成补勤录入单
					insert into bd_repair_attendance(order_id,order_no,start_date,end_date,people_num,remark,creater_id,create_time)
					values (@id,@number,@date,@date,@user_count,'自动补出勤','',getdate());

					-- 单单的上班补出勤数据
					insert into lr_jl(sj,user_serial,bz,order_id,whether_validity)
					select concat(@date,' ',dbo.bd_fn_numToMin(shift_begin),':00'),a.user_serial,'自动补出勤-上班',@id,1
					from bd_kq_schedule_detail a
					inner join #user_serial b on a.user_serial = b.user_serial and a.shift_date = @date

					-- 单单的上班补出勤数据
					insert into lr_jl(sj,user_serial,bz,order_id,whether_validity)
					select concat(@date,' ',dbo.bd_fn_numToMin(shift_end),':00'),a.user_serial,'自动补出勤-下班',@id,1
					from bd_kq_schedule_detail a
					inner join #user_serial b on a.user_serial = b.user_serial and a.shift_date = @date

					-- 上班打卡的数据（包含补出勤）
					insert into kt_jl(sj,user_serial,fx,lx,whether_validity,mode,order_id)
					select concat(@date,' ',dbo.bd_fn_numToMin(shift_begin),':00'),a.user_serial,0,0,1,1,@id
					from bd_kq_schedule_detail a
					inner join #user_serial b on a.user_serial = b.user_serial and a.shift_date = @date

					-- 下班打卡的数据（包含补出勤）
					insert into kt_jl(sj,user_serial,fx,lx,whether_validity,mode,order_id)
					select concat(@date,' ',dbo.bd_fn_numToMin(shift_end),':00'),a.user_serial,1,0,1,1,@id
					from bd_kq_schedule_detail a
					inner join #user_serial b on a.user_serial = b.user_serial and a.shift_date = @date



          declare @user_serial_str varchar(max) = '';
          select @user_serial_str += iif(isnull(user_serial,'') = '','',concat(',',user_serial)) from #user_serial

          -- 去除最前面的逗号
          set @user_serial_str = iif(left(@user_serial_str,1) = ',',right(@user_serial_str,len(@user_serial_str)-1),@user_serial_str)

          --计算考勤(1代表补出勤)
          EXEC bd_proc_timecard @date, @user_serial_str, 1

				END

		--  提交事务
			COMMIT TRAN

	--  结束异常捕捉
		END TRY

	--  异常处理
		BEGIN CATCH
			DECLARE @errStr VARCHAR(MAX) = ERROR_MESSAGE();
			IF XACT_STATE() <> 0
			BEGIN
			  ROLLBACK TRANSACTION;
			  RAISERROR (@errStr, 16, 1);
			END;
			ROLLBACK TRAN;
		END CATCH



-- 打开打印受影响行数
SET NOCOUNT OFF;
END;
